#!/bin/bash

# 更新系统并安装基本包
echo "Updating system and installing basic packages..."
sudo apt update
sudo apt upgrade -y
sudo apt install -y git cmake build-essential

# 编译并安装 fmt-8.1.0
echo "Downloading and installing fmt-8.1.0..."
wget https://github.com/fmtlib/fmt/archive/refs/tags/8.1.0.tar.gz
tar -xzvf 8.1.0.tar.gz
cd fmt-8.1.0
mkdir build
cd build
cmake .. -DBUILD_SHARED_LIBS=ON -DFMT_TEST=OFF -DFMT_DOC=OFF
make -j$(nproc)
sudo make install
cd ../..

# 编译并安装 spdlog
echo "Downloading and installing spdlog..."
wget https://github.com/gabime/spdlog/archive/refs/tags/v1.9.2.tar.gz
tar -xzvf v1.9.2.tar.gz
cd spdlog-1.9.2
mkdir build
cd build
cmake .. -DSPDLOG_BUILD_SHARED=ON -DSPDLOG_BUILD_TESTS=OFF -DSPDLOG_BUILD_EXAMPLE=OFF
make -j$(nproc)
sudo make install
cd ../..

# 从源码安装 Boost 1.81.0
echo "Downloading and installing Boost 1.81.0 from source..."
wget https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.gz
tar -xzvf boost_1_81_0.tar.gz
cd boost_1_81_0
./bootstrap.sh
sudo ./b2 install
cd ..

# 安装 nanomsg
echo "Installing nanomsg..."
sudo apt install -y libnanomsg-dev

# 安装 rapidjson
echo "Installing rapidjson..."
sudo apt install -y rapidjson-dev

# 设置交换空间（如果内存小于2GB）
echo "Setting up swap file..."
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile swap swap defaults 0 0" | sudo tee -a /etc/fstab

# 克隆并编译 wondertrader
echo "Cloning and compiling wondertrader..."
git clone https://github.com/cloudQuant/wondertrader.git
cd wondertrader/src
sh build_release.sh
cd ../..

# 克隆 demos 并更新文件
echo "Cloning demos and updating files..."
git clone https://gitee.com/yunjinqi/demos.git
chmod -R +x demos
cd demos
sh update_files.sh
cd ..
